<?php

/**
 * This is the model class for table "{{answer}}".
 *
 * The followings are the available columns in table '{{answer}}':
 * @property integer $ID
 * @property integer $issueID
 * @property integer $thread_number
 * @property integer $resolver
 * @property integer $editor
 * @property integer $resolve_status
 * @property integer $edit_status
 * @property string $content
 * @property string $comment
 * @property integer $createddate
 * @property integer $modifieddate
 * @property integer $assign_edit_date
 * @property integer $editeddate
 * @property integer $deleted
 */
class Answer extends CActiveRecord
{
	public $image;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Answer the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{answer}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('issueID, thread_number, resolver, editor, resolve_status, edit_status, content, comment, createddate, modifieddate, assign_edit_date, editeddate, deleted', 'required'),
			array('issueID, thread_number, resolver, editor, resolve_status, edit_status, createddate, modifieddate, assign_edit_date, editeddate, deleted', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ID, issueID, thread_number, resolver, editor, resolve_status, edit_status, content, comment, createddate, modifieddate, assign_edit_date, editeddate, deleted', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ID' => 'ID',
			'issueID' => 'Issue',
			'thread_number' => 'Thread Number',
			'resolver' => 'Người giải',
			'editor' => 'Người soạn',
			'resolve_status' => 'Trạng thái giải',
			'edit_status' => 'Trạng thái soạn thảo',
			'content' => 'Nội dung',
			'comment' => 'Chú thích',
			'createddate' => 'Ngày tạo',
			'modifieddate' => 'Ngày cập nhật',
			'assign_edit_date' => 'Ngày phân công',
			'editeddate' => 'Ngày cập nhật',
			'deleted' => 'Xóa?',
		);
	}

 protected function beforeSave() {
		if($this->isNewRecord) {

			$this->deleted = 0;					
			$this->createddate =time();	
		}
		
		$this->modifieddate = time();
		return parent::beforeSave();
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ID',$this->ID);
		$criteria->compare('issueID',$this->issueID);
		$criteria->compare('thread_number',$this->thread_number);
		$criteria->compare('resolver',$this->resolver);
		$criteria->compare('editor',$this->editor);
		$criteria->compare('resolve_status',$this->resolve_status);
		$criteria->compare('edit_status',$this->edit_status);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('comment',$this->comment,true);
		$criteria->compare('createddate',$this->createddate);
		$criteria->compare('modifieddate',$this->modifieddate);
		$criteria->compare('assign_edit_date',$this->assign_edit_date);
		$criteria->compare('editeddate',$this->editeddate);
		$criteria->compare('deleted',$this->deleted);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	//function getGrade
	public function getResolving() {
		$array = array();
		$array[32] = "Resolve";
		$array[33] = "Cannot Resolve"; 
		$array[34] = "Invalid";
		return $array;
	}
	
	
	
	/**
	 * Cofirm answer status
	 * @author khatv
	 */
	public function confirmStatus($type)
	{
		 if($type=='resolve')
		 {
		 	 $status=$this->resolve_status;
		 }
		 else
		 {
		 	 $status=$this->edit_status;
		 }		
		 // resolve
		 if($status==32)
     {
    	 $confirm=38;	
     }
     // cannot resolve
     else if($status==33)
     {
    	 $confirm=39;	
     } 
     // invalid
     else if($status==34)
     {
     	$confirm=39;	
     } 
     //timeout
     else if($status==35)
     {
     		$confirm=39;		
     }
     else {
     	
     	return ;
     }
     
     if($type=='resolve')
		 {
		 	 $this->resolve_status =$confirm ;
		 }
		 else
		 {
		 	 $this->edit_status =$confirm ;
		 }		 
	}
	
	/**
	 * Get Ansers of user
	 * Enter description here ...
	 */
	public function getAnswers()
	{
		//action for index, list all thread of each issue
	  $criteria=new CDbCriteria;
	  $criteria->condition = "t.deleted=0";
	  $userID =Yii::app()->user->id;		
	  $criteria->select = "t.*";
	  $criteria->join = "JOIN tbl_issue as i ON t.issueID=i.ID";
	  $criteria->addCondition("i.userID=".Yii::app()->user->id);
	  
	  return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
}